<template>
  <div>
      <el-dialog
  :title="obj.isAdd?'活动添加':'活动编辑'"
  :visible.sync="obj.isShow"
  width="30%">
  <el-form>
     <el-form-item label="活动名称" label-width="80px">
    <el-input v-model="form.title" ></el-input>
  </el-form-item>
    <el-form-item label="活动期限" label-width="80px">
      <el-date-picker
                v-model="value1"
                type="datetimerange"
                range-separator="到"
                start-placeholder="开始"
                end-placeholder="结束">
</el-date-picker>
<!-- <el-button @click="get()">获取</el-button>
<el-button @click="set()">设置</el-button> -->
    </el-form-item>
      <el-form-item label="一级分类">
    <el-select v-model="form.first_cateid" placeholder="请选择一级分类" @change="changecate()">
      <el-option v-for="item in CateList" :key="item.id" :label="item.catename" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
    <el-form-item label="二级分类">
    <el-select v-model="form.second_cateid" placeholder="请选择二级分类" @change="changegoods()">
      <el-option v-for="item in secondlist" :key="item.id" :label="item.catename" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
    <el-form-item label="选择商品">
    <el-select v-model="form.goodsid" placeholder="请选择活动区域">
      <el-option v-for="item in goodslist" :key="item.id" :label="item.goodsname" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="状态">
    <el-switch v-model="form.delivery" :active-value="1" :inactive-value="2"></el-switch>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="cancel()">取 消</el-button>
    <el-button type="primary" @click="add()" v-if="obj.isAdd">添 加</el-button>
    <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {cateList,goodsList,seckadd,seckinfo,seckedit} from "../../../request/api.js"
import { warningAlert,sucessAlert } from '../../../utils/alert';
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
        //时间
        value1:[],
        //二级分类
        secondlist:[],
        //选择商品
        goodslist:[],
         form: {
          title: '',
          begintime: '',
          endtime: '',
          first_cateid: '',
          second_cateid:'',
          goodsid:'',
          status: 1
    }
  }
  },
  computed: {
    ...mapGetters({
        CateList:"cate/list"
    }),
  },
  methods: {
    ...mapActions({
         CateAsyncList:"cate/acyncChangeList",
          seckAsyncList:"seckill/asynclist"
    }),
   //二级分类
    changecate(){
        cateList({pid:this.form.first_cateid}).then(res=>{
            this.form.second_cateid=''
            this.secondlist = res.data.list
        })
    },
    //商品
    changegoods(){
      goodsList().then(res=>{
        this.form.goodsid = ''
      let goodsoption = res.data.list.filter(item=>{
          return this.form.second_cateid == item.second_cateid
        })
         this.goodslist = goodsoption
      })
    },
    //清空
    empty(){
      //时间
        this.value1=[],
        //二级分类
        this.secondlist=[],
        //选择商品
        this.goodslist=[],
         this.form= {
          title: '',
          begintime: '',
          endtime: '',
          first_cateid: '',
          second_cateid:'',
          goodsid:'',
          status: 1
    }
    },
    //取消
    cancel(){
      this.obj.isShow = false
      this.empty()
    },
    //添加
    add(){
      this.form.begintime = new Date(this.value1[0]).getTime()
      this.form.endtime = new Date(this.value1[1]).getTime()
      console.log(this.form)
      // console.log(this.value1)
      seckadd(this.form).then(res=>{
          if(res.data.code == 200){
          sucessAlert(res.data.msg),
          this.cancel()
          this.seckAsyncList()
        }else{
          // 失败的提示窗
          warningAlert(res.data.msg)
        }
      })
    },
    getOne(id){
      seckinfo({id:id}).then(res=>{
          console.log(res.data.list)
          let obj = res.data.list
          this.form = obj
          this.value1.push(Number(obj.begintime))
           this.value1.push(Number(obj.endtime))
        this.form.id = id
      })
    },
    edit(){
      this.form.begintime = new Date(this.value1[0]).getTime()
      this.form.endtime = new Date(this.value1[1]).getTime()
      seckedit(this.form).then(res=>{
         if(res.data.code == 200){
          sucessAlert(res.data.msg),
          this.cancel()
          this.seckAsyncList()
        }else{
          // 失败的提示窗
          warningAlert(res.data.msg)
        }
      })
    }
  },
  mounted() {
      this.CateAsyncList()
  },
};
</script>
<style scoped>
</style>